package com.yutel.test.ip;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.Security;

public class DnsCache {
	public static void main(String[] args) {
		DnsCache dc = new DnsCache();
		dc.testDomain("1", "epg.funhd.cn");
		// dc.testURL("5",
		// "http://epg.funhd.cn/epg/20130513145042/channels-categories.xml");
	}

	public void testURL(String ttl, String urls) {
		Security.setProperty("networkaddress.cache.ttl", ttl);
		Security.setProperty("networkaddress.cache.negative.ttl", ttl);
		long start = System.currentTimeMillis();
		long midle = 0;
		long end = 0;
		int clenght = 0;
		try {
			URL url = new URL(urls);
			HttpURLConnection uc = (HttpURLConnection) url.openConnection();
			uc.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			uc.setRequestMethod("GET");
			uc.setConnectTimeout(1000);
			uc.setReadTimeout(1000);
			midle = System.currentTimeMillis();
			clenght = uc.getContentLength();
			end = System.currentTimeMillis();
			System.out.println("url1:open=" + (midle - start) + ",get="
					+ (end - midle));
			System.out.println("uc1=" + clenght + ",ip="
					+ uc.getInputStream().toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.print("按任意键继续");
		try {
			System.in.read();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		try {
			start = System.currentTimeMillis();
			URL url2 = new URL(urls);
			HttpURLConnection uc2 = (HttpURLConnection) url2.openConnection();
			uc2.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			uc2.setRequestMethod("GET");
			uc2.setConnectTimeout(1000);
			uc2.setReadTimeout(1000);
			midle = System.currentTimeMillis();
			clenght = uc2.getContentLength();
			end = System.currentTimeMillis();
			System.out.println("url2:open=" + (midle - start) + ",get="
					+ (end - midle));
			System.out.println("uc2=" + clenght + ",ip="
					+ uc2.getInputStream().toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void testDomain(String ttl, String domain) {
		java.security.Security.setProperty("networkaddress.cache.ttl", ttl);
		long time = System.currentTimeMillis();
		InetAddress addresses1[];
		try {
			addresses1 = InetAddress.getAllByName(domain);
			System.out.println("addresses1:"
					+ (System.currentTimeMillis() - time) + "毫秒");
			for (InetAddress address : addresses1)
				System.out.println(address);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}

		System.out.print("按任意键继续");
		try {
			System.in.read();
		} catch (IOException e) {
			e.printStackTrace();
		}
		time = System.currentTimeMillis();
		InetAddress addresses2[];
		try {
			addresses2 = InetAddress.getAllByName(domain);
			System.out.println("addresses2:"
					+ (System.currentTimeMillis() - time) + "毫秒");
			for (InetAddress address : addresses2)
				System.out.println(address);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
}
